package com.baizhi.service.impl;

import com.baizhi.dao.*;
import com.baizhi.entity.*;
import com.baizhi.service.CmfzRoleService;
import com.baizhi.util.ResourceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2019/4/19.
 */
@Service
public class CmfzRoleServiceImpl implements CmfzRoleService {
    @Autowired
    private CmfzAdminDao cmfzAdminDao;
    @Autowired
    private CmfzAdminRoleDao cmfzAdminRoleDao;
    @Autowired
    private CmfzRoleDao cmfzRoleDao;
    @Autowired
    private CmfzRoleResourceDao cmfzRoleResourceDao;
    @Autowired
    private CmfzResourceDao cmfzResourceDao;

    @Override
    public Set<String> getAllRolesByUsername(String username) {
        CmfzAdmin cmfzAdmin = cmfzAdminDao.selectOne(new QueryWrapper<CmfzAdmin>().eq("username", username));
        List<CmfzAdminRole> cmfzAdminRoleList = cmfzAdminRoleDao.selectList(new QueryWrapper<CmfzAdminRole>().eq("admin_id", cmfzAdmin.getId()));
        Set<String> roles = new HashSet<>();
        for (CmfzAdminRole cmfzAdminRole : cmfzAdminRoleList) {
            CmfzRole cmfzRole = cmfzRoleDao.selectById(cmfzAdminRole.getRoleId());
            roles.add(cmfzRole.getRoleName());
        }
        return roles;
    }

    @Override
    public Set<String> getAllPermissionsByUsername(String username) {
        Set<String> allRolesByUsername = getAllRolesByUsername(username);
        Set<String> permissions = new HashSet<>();
        for (String roleName : allRolesByUsername) {
            List<CmfzRoleResource> roleResources = cmfzRoleResourceDao.selectList(new QueryWrapper<CmfzRoleResource>().eq("role_name", roleName));
            for (CmfzRoleResource roleResource : roleResources) {
                CmfzResource cmfzResource = cmfzResourceDao.selectOne(new QueryWrapper<CmfzResource>().eq("resource_id",roleResource.getResourceId()).eq("resource_type","function"));
                if (cmfzResource != null) {
                    permissions.add(cmfzResource.getResourcePermission());
                }
            }
        }
        return permissions;
    }


    @Override
    public List<CmfzResource> selectAllResource() {
//        1.查询所有一级资源
        List<CmfzResource> cmfzResources = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().isNull("resource_parent_id"));
//        2.调用其他方法
        ResourceUtil.getParentResource(cmfzResources,cmfzResourceDao);

        return cmfzResources;
    }

    @Override
    public Map selectRoleByPage(Integer page, Integer limit) {
        Map map = new HashMap();
//        数据响应成功为0 layui会这个值
        map.put("code",0);
//        msg:当后台响应不正常的时候的信息描述 如果后台响应状态是0，这个属性的值可以写””
        map.put("msg","");
        IPage<CmfzRole> cmfzBannerIPage = cmfzRoleDao.selectPage(new Page<CmfzRole>(page, limit), null);
        long total = cmfzBannerIPage.getTotal();
        List<CmfzRole> records = cmfzBannerIPage.getRecords();
//        count:获取总条数
        map.put("count",total);
//        data:获取当前页数据
        map.put("data",records);
        return map;
    }
}
